import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

public class MulticastSender {
	
	private static List<String> list;
	static {
		list = new ArrayList<>();
		try (Scanner cin = new Scanner(new BufferedInputStream(new FileInputStream("copy")))) {
			String line = null;
			while(cin.hasNext()) {
				line = cin.nextLine();
				list.add(line);
			}
			list.add("no data");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args){
		MulticastSocket ms = null;
		try {
			InetAddress group = InetAddress.getByName("228.0.0.1");
			int port = 4000;
			ms = new MulticastSocket(port);
			while(true){
				Iterator<String> iterator = list.iterator();
				while(iterator.hasNext()) {
					String message =iterator.next();
					byte[] bytes = message.getBytes();
					DatagramPacket pd = new DatagramPacket(bytes, bytes.length, group, port);
					ms.send(pd);
					System.out.println("已发送数据包：     " + message);
				}
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if(ms != null)
				ms.close();
		}
	}
}